// 游戏入口文件
import { GameController } from './model/GameController'
import { GRID_SIZE, CELL_SIZE } from './constants/gameConstants'

// 创建游戏画布
const canvas = wx.createCanvas()
const ctx = canvas.getContext('2d')

// 获取系统信息
const systemInfo = wx.getSystemInfoSync()
canvas.width = systemInfo.windowWidth
canvas.height = systemInfo.windowHeight

// 计算游戏区域的大小和位置
const gameWidth = GRID_SIZE * CELL_SIZE
const gameHeight = GRID_SIZE * CELL_SIZE
const offsetX = (canvas.width - gameWidth) / 2
const offsetY = (canvas.height - gameHeight) / 2

// 初始化游戏控制器
const gameController = new GameController(ctx, canvas.width, canvas.height, offsetX, offsetY)

// 游戏主循环
function gameLoop() {
  gameController.update()
  gameController.render()
  requestAnimationFrame(gameLoop)
}

// 开始游戏循环
gameLoop()

// 监听触摸事件
wx.onTouchStart((e) => {
  gameController.handleTouchStart(e)
})

wx.onTouchMove((e) => {
  gameController.handleTouchMove(e)
})

wx.onTouchEnd((e) => {
  gameController.handleTouchEnd(e)
})

// 监听键盘事件
wx.onKeyDown((res) => {
  console.log('Key pressed:', res.key) // 添加日志
  switch (res.key) {
    case 'ArrowUp':
    case 'Up':
      console.log('Up pressed')
      gameController.handleKeyPress('UP')
      break
    case 'ArrowDown':
    case 'Down':
      console.log('Down pressed')
      gameController.handleKeyPress('DOWN')
      break
    case 'ArrowLeft':
    case 'Left':
      console.log('Left pressed')
      gameController.handleKeyPress('LEFT')
      break
    case 'ArrowRight':
    case 'Right':
      console.log('Right pressed')
      gameController.handleKeyPress('RIGHT')
      break
  }
})
